package renguanyu.app.tools.controller;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.mysql.cj.jdbc.MysqlDataSource;

import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.util.Callback;
import renguanyu.app.tools.model.anjuke.AjkShDao;
import renguanyu.app.tools.model.anjuke.BhmsDao;
import renguanyu.app.tools.model.anjuke.CommunityUtil;
import renguanyu.app.tools.util.DbUtil;
import renguanyu.app.tools.util.SystemUtil;

public class AnjukeDataAnalyzeTableController {

	int selectRowNumber = 0;

	@FXML
	TextField dbUrl;
	@FXML
	TextField dbUser;
	@FXML
	TextField dbPassword;
	@FXML
	TextField communityName;
	@FXML
	TableView<Map<String, Object>> dataTable;
	@FXML
	Button testConnectionBtn;
	@FXML
	Button randomUserAgentBtn;
	@FXML
	Button queryDataBtn;
	@FXML
	Button previewDataBtn;
	@FXML
	ComboBox<String> community;
	@FXML
	TableColumn<Map<String, Object>, String> code;
	@FXML
	TableColumn<Map<String, Object>, String> title;
	@FXML
	TableColumn<Map<String, Object>, String> structure;
	@FXML
	TableColumn<Map<String, Object>, String> area;
	@FXML
	TableColumn<Map<String, Object>, String> totalPrice;
	@FXML
	TableColumn<Map<String, Object>, String> avgPrice;
	@FXML
	TableColumn<Map<String, Object>, String> url;
	@FXML
	TableColumn<Map<String, Object>, String> toward;
	@FXML
	TableColumn<Map<String, Object>, String> floor;
	@FXML
	TableColumn<Map<String, Object>, String> commAddress;
	@FXML
	Button previewSelRowDataBtn;
	@FXML
	TableColumn<Map<String, Object>, Integer> order;
	@FXML
	Button queryLowAvgPriceBtn;
	@FXML
	Text pvn;

	@FXML
	public void initialize() {
		community.getItems().addAll(CommunityUtil.data);
		community.setValue(community.getItems().get(0));

		order.setCellFactory(new IDCell<>());
		code.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("code")));
		title.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("name")));
		structure.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("structure")));
		area.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("area")));
		totalPrice.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("total_price")));
		avgPrice.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("avg_price")));
		url.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("url")));
		toward.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("toward")));
		floor.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("floor")));
		commAddress.setCellValueFactory(param -> new ReadOnlyObjectWrapper(param.getValue().get("community_address")));

	}

	@FXML
	public void testConnection() throws SQLException {

		MysqlDataSource ds = DbUtil.getDataSource();
		ds.setUrl(dbUrl.getText());
		ds.setUser(dbUser.getText());
		ds.setPassword(dbPassword.getText());

		QueryRunner r = DbUtil.getQueryRunner();
		Long n = r.query("select 1", new ScalarHandler<Long>());

		if (n != null) {
			Alert alert = new Alert(Alert.AlertType.INFORMATION);
			alert.setTitle("测试连接");
			alert.setHeaderText("SUCCESS");
			alert.show();
		}
	}

	AjkShDao ajkShDao = new AjkShDao();
	BhmsDao bhmsDao = new BhmsDao();

	@FXML
	public void queryData() throws SQLException {
		String communityName = community.getSelectionModel().getSelectedItem();

		List<Map<String, Object>> listMap = ajkShDao.queryDayNew(communityName);
		dataTable.getItems().setAll(listMap);

		Integer total = bhmsDao.queryPvn(communityName);
		pvn.setText("民声数：" + total);
	}

	@FXML
	public void previewData() {
		// 选中下一行
		if (selectRowNumber >= dataTable.getItems().size()) {
			selectRowNumber = 0;
		}
		dataTable.getSelectionModel().select(selectRowNumber++);
		// 获取参数
		Map<String, Object> selectedItem = dataTable.getSelectionModel().getSelectedItem();

		SystemUtil.copyString(selectedItem.get("url").toString());
	}

	@FXML
	public void previewSelRowData() {
		Map<String, Object> selectedItem = dataTable.getSelectionModel().getSelectedItem();
		SystemUtil.copyString(selectedItem.get("url").toString());
	}

	public class IDCell<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {

		@Override
		public TableCell<S, T> call(TableColumn<S, T> param) {

			TableCell cell = new TableCell() {
				
				@Override
				protected void updateItem(Object item, boolean empty) {
					super.updateItem(item, empty);
					this.setText(null);
					this.setGraphic(null);
					if (!empty) {
						int rowIndex = this.getIndex() + 1;
						this.setText(String.valueOf(rowIndex));
					}
				}

			};
			return cell;
		}

	}

	@FXML
	public void queryLowAvgPrice() throws SQLException {
		String communityName = community.getSelectionModel().getSelectedItem();

		List<Map<String, Object>> listMap = ajkShDao.queryLowAvgPrice(communityName);
		dataTable.getItems().setAll(listMap);

		Integer total = bhmsDao.queryPvn(communityName);
		pvn.setText("民声数：" + total);
	}

	@FXML
	public void queryLowTotalAvgPrice() throws SQLException {

		String communityName = community.getSelectionModel().getSelectedItem();

		List<Map<String, Object>> listMap = ajkShDao.queryLowTotalAvgPrice(communityName);
		dataTable.getItems().setAll(listMap);

		Integer total = bhmsDao.queryPvn(communityName);
		pvn.setText("民声数：" + total);
	}

}
